package com.hippo.core.ui.compose.mine

import android.net.Uri
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LifecycleEventEffect
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import coil.compose.rememberAsyncImagePainter
import com.hippo.core.R
import com.hippo.core.core.ext.click
import com.hippo.core.core.ext.paddingBody
import com.hippo.core.ui.components.HorizontalSpacer
import com.hippo.core.ui.components.TextLayout
import com.hippo.core.ui.components.VerticalSpacer
import com.hippo.core.ui.navigation.AppRoute
import com.hippo.core.ui.theme.ColorBackgroundGray
import com.hippo.core.ui.theme.ColorTextBlack
import com.hippo.core.ui.theme.HippoTheme
import com.hippo.core.ui.theme.Shapes
import com.hippo.core.utils.UriUtils
import com.hippo.core.vm.MineVM

@Composable
fun MineScreen(navController: NavController, vm: MineVM = viewModel()) {

    val uiState = vm.uiState


    val context = LocalContext.current

    val launcher = rememberLauncherForActivityResult(
        contract = ActivityResultContracts.GetContent()
    ) { uri: Uri? ->

        if(uri != null){
            vm.updateState(uiState.copy(localUri = uri))

            val path = UriUtils.getRealPathFromURI(context, uri)
            vm.updateAvatar(path.orEmpty())
        }
    }

    var showChangeNameDialog by remember { mutableStateOf(false) }

    if (showChangeNameDialog) {
        ChangeNameDialog(
            onDismissRequest = { showChangeNameDialog = false },
            onSave = { name ->
                vm.updateNickName(name)
            }
        )
    }

    LifecycleEventEffect(Lifecycle.Event.ON_CREATE) {
        vm.getUserInfo()
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .verticalScroll(state = rememberScrollState())
            .paddingBody()

    ) {

        VerticalSpacer(36.dp, modifier = Modifier.weight(1F))

        val painter = if (uiState.localUri != null) {
            rememberAsyncImagePainter(uiState.localUri)
        }else if (uiState.userInfo?.avatar != null) {
            rememberAsyncImagePainter(
                uiState.userInfo.avatar,
                placeholder = painterResource(id = R.mipmap.mine_camera),
                error = painterResource(id = R.mipmap.mine_camera)
            )
        } else {
            painterResource(id = R.mipmap.mine_camera)
        }

        Box(
            modifier = Modifier
                .align(alignment = Alignment.CenterHorizontally)
                .click {
                    launcher.launch("image/*")
                }
        ){
            Image(
                painter = painter,
                contentDescription = null,
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .size(80.dp)
                    .clip(CircleShape)
                    .click {
                        launcher.launch("image/*")
                    }
            )

            Image(
                painter = painterResource(id = R.mipmap.take),
                contentDescription = null,
                modifier = Modifier
                    .size(30.dp)
                    .align(Alignment.BottomEnd)
            )

        }



        VerticalSpacer(23.dp)

        Text(
            modifier = Modifier.align(Alignment.CenterHorizontally),
            text = uiState.userInfo?.userEmail.orEmpty(),
            style = TextStyle(
                fontWeight = FontWeight.Bold,
                fontSize = 16.sp
            ),
            color = ColorTextBlack
        )

        VerticalSpacer(14.dp)


        Box(
            modifier = Modifier
                .fillMaxWidth()
                .background(
                    color = ColorBackgroundGray,
                    shape = Shapes.extraLarge
                )
                .padding(12.dp)
        ) {
            Text(
                modifier = Modifier.align(alignment = Alignment.Center),
                text = uiState.userInfo?.nickName.orEmpty(),
                style = TextStyle(
                    fontWeight = FontWeight.Bold,
                    fontSize = 16.sp
                ),
                color = ColorTextBlack
            )


            Image(
                painter = painterResource(id = R.mipmap.mine_edit),
                contentDescription = null,
                modifier = Modifier
                    .align(alignment = Alignment.CenterEnd)
                    .size(25.dp)
                    .click {
                        showChangeNameDialog = true
                    }
            )

        }

        VerticalSpacer(14.dp)

        Row(
            modifier = Modifier
                .align(Alignment.CenterHorizontally)
        ) {
            Image(
                painter = painterResource(id = R.mipmap.location),
                contentDescription = null,
                modifier = Modifier
                    .size(14.dp)
            )
            HorizontalSpacer(5.dp)

            Text(
                modifier = Modifier,
                text = uiState.userInfo?.countryName.orEmpty(),
                style = TextStyle(
                    fontSize = 14.sp
                ),
                color = Color.Gray
            )
        }

        VerticalSpacer(36.dp, modifier = Modifier.weight(1F))

        TextLayout(
            leftPainter = painterResource(id = R.mipmap.home_msg),
            text = stringResource(R.string.me_3),
            onClick = {
                navController.navigate(AppRoute.NotificationScreen)
            }
        )

        TextLayout(
            leftPainter = painterResource(id = R.mipmap.mine_setting),
            text = stringResource(R.string.me_2),
            onClick = {
                navController.navigate(AppRoute.SettingScreen)
            }
        )

        TextLayout(
            leftPainter = painterResource(id = R.mipmap.mine_about),
            text = stringResource(R.string.me_1),
            onClick = {
                navController.navigate(AppRoute.AboutScreen)
            }
        )

        VerticalSpacer(36.dp, modifier = Modifier.weight(3F))

    }

}

@Preview(showBackground = true)
@Composable
private fun MineScreenPreview() {
    HippoTheme {
        MineScreen(
            navController = rememberNavController()
        )
    }
}
